package com.yingjie.customcontrol;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import com.yingjie.addressbook.DB;
import com.yingjie.addressbook.DBReturn;
import com.yingjie.addressbook.G;

import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Toast;

@SuppressWarnings("hiding")
public class AutoCompleteAdapter<String> extends ArrayAdapter<String>
{
	private AutoFilter _customFilter = null;
	Context context;
	DBReturn dbReturn;
	public Filter getFilter() {
	    if (_customFilter == null)
	        _customFilter = new AutoFilter();
	    return _customFilter;
	}
	
	public AutoCompleteAdapter(Context context, int textViewResourceId) {
	    super(context, textViewResourceId);
	    this.context = context;
	}
	
	public class AutoFilter extends Filter {
		@Override
	    protected FilterResults performFiltering(CharSequence constraint) {
	        FilterResults fr = null;
	        if (constraint != null)
	        {
	            List<String> list = getFilterdValues((String) constraint.toString());

	            if (list != null)
	            {
	                fr = new FilterResults();
	                fr.values = list;
	                fr.count = list.size();
	            }
	        }
	        return fr;
	    }
		
    	private List<String> getFilterdValues(String s){
    		List<String> list = new ArrayList<String>();
    		G g = (G)context.getApplicationContext();
    		JSONArray jaAutoData = this.getAuto(s, 5, g.getUserInfo().CompanyId, g.getUserInfo().UserId, 2);
    		int iCount = jaAutoData.length();
    		list.clear();
    		//strAutoData = new String[iCount];
    		for(int i = 0; i < iCount;i ++){
    			JSONArray jaRow;
				try {
					jaRow = jaAutoData.getJSONArray(i);
					list.add((String) jaRow.getString(1));
				} catch (JSONException e) {
					e.printStackTrace();
				}
    		}
    		return list;
    	}
    	
    	private JSONArray getAuto(String strSearch,int qty,int cid,int uid,int type){
    		JSONArray para = new JSONArray();
    		para.put(strSearch);
    		para.put(qty);
    		para.put(cid);
    		para.put(uid);
    		para.put(type);
    		para.put(0);
    		dbReturn = DB.noPagination_New(context.getApplicationContext(), "up_autoComplete2", para, 10);
    		JSONArray jaReturn = dbReturn.OutputTable;
    		return jaReturn;
    	}
		
		@Override
	    protected void publishResults(CharSequence constraint,
	            FilterResults results) {
			if(dbReturn != null && dbReturn.ISSuccess != 1){
				Toast.makeText(context, dbReturn.getDisplayMessage(), Toast.LENGTH_SHORT).show();
			}
	        AutoCompleteAdapter.this.clear();
	        if (results != null)
	        {
	            if (results.values != null)
	            {
	                List<String> items = (List<String>) results.values;

	                    for (String listItem : items) {
	                        AutoCompleteAdapter.this.add(listItem);
	                    }

	                if (items.size() > 0)
	                {
	                    AutoCompleteAdapter.this.notifyDataSetChanged();
	                    return;
	                }
	            }
	        }
	        AutoCompleteAdapter.this.notifyDataSetInvalidated();
	    }
		
	}
}